﻿using System;
using System.Collections.Generic;
using Moq;

namespace PoolKit.Tests
{
    internal class GenericPoolCollaboratorsFactory
    {
        public static ICommandProcessor CreateDefaultCommandProcessor()
        {
            return new CommandProcessorThreadPoolBased();
        }

        public static GenericPool<TResource> CreateDefaultGenericPool<TResource>(IEnumerable<TResource> resources)
            where TResource : class
        {
            var resourceCollection = new ResourceCollection<TResource>(resources);
            var reusePreparationStrategy = new Mock<ReusePreparationStrategy<TResource>>();
            var repairStrategy = new Mock<RepairStrategy<TResource>>();

            var commandProcessor = CreateDefaultCommandProcessor();

            try
            {
                return new GenericPool<TResource>(resourceCollection, reusePreparationStrategy.Object, repairStrategy.Object, commandProcessor);
            }
            catch (Exception)
            {
                commandProcessor.Dispose();
                throw;
            }
        }
    }
}